//
//  ConExchangeListController.m
//  Shengzhuli
//
//  Created by FXGuo on 2017/9/6.
//  Copyright © 2017年 kamangkeji. All rights reserved.
//

#import "ConExchangeListController.h"
#import "ExchangeListCell.h"
#import "ConExchangeListDetailController.h"
#import "ActivityNetWork.h"
#import "SZLAccountHelper.h"
#import "MBProgressHUD+MJ.h"
#import "ActivityBillModel.h"
#import <MJRefresh.h>

@interface ConExchangeListController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, assign) int pageNo;

@end

@implementation ConExchangeListController

- (void)viewDidLoad {
    [super viewDidLoad];
    _pageNo = 1;
    _dataArray = [NSMutableArray array];
    [self loadData];
    self.title = @"兑换清单";
    [self.view addSubview:self.tableView];
}
- (void)loadData {
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"pageNo"] = @(_pageNo);
    param[@"token"] = [SZLAccountHelper shareInstance].token;
    [ActivityNetWork getHaveChangedGoodsListWithParame:param response:^(BOOL success, NSString *msg, NSArray *moneyArr) {
        
        if (success) {
            [_tableView.mj_footer endRefreshingWithNoMoreData];
            [_tableView.mj_header endRefreshing];
            for (NSDictionary *dict in moneyArr) {
                ActivityBillModel *model = [[ActivityBillModel alloc] init];
                [model setModelWithDict:dict];
                
                [_dataArray addObject:model];
            }
            [_tableView reloadData];
        }else {
            [MBProgressHUD showError:msg];
        }
        
    }];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ExchangeListCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ExchangeListCell class])];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    ActivityBillModel *model = _dataArray[indexPath.row];
    [cell updateCellWithModel:model];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ConExchangeListDetailController *vc = [[ConExchangeListDetailController alloc]init];
    ActivityBillModel *model = _dataArray[indexPath.row];
    vc.orderNo= model.orderNo;
    vc.imageString = model.thumbnailUrl;
    [self.navigationController pushViewController:vc animated:YES];
}

- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenW,ScreenH-64)];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.backgroundColor = RGBColor(239, 239, 244);
        _tableView.rowHeight = 130;
        _tableView.tableFooterView = [[UIView alloc]init];
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([ExchangeListCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([ExchangeListCell class])];
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            _pageNo = 1;
            [_dataArray removeAllObjects];
            [self loadData];
        }];
        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            _pageNo++;
            [self loadData];
        }];
    }
    return _tableView;
}


@end
